class PayController {

    def payService

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ payInstanceList: Pay.list( params ) ]
    }

    def show = {
        def payInstance = Pay.get( params.id )

        if(!payInstance) {
            flash.message = "Pay not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ payInstance : payInstance ] }
    }

    def delete = {
        def payInstance = Pay.get( params.id )
        if(payInstance) {
            payInstance.delete()
            flash.message = "Pay ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Pay not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def payInstance = Pay.get( params.id )

        if(!payInstance) {
            flash.message = "Pay not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ payInstance : payInstance ]
        }
    }

    def create = {
        def payInstance = new Pay()
        payInstance.properties = params
        return ['payInstance':payInstance]
    }

    def save = {
        def payInstance = new Pay(params)
        if(!payInstance.hasErrors()) {
            payService.savePayAndUpdateBalance(payInstance)
            flash.message = "Pay ${payInstance.id} created"
            //redirect(action:show,id:payInstance.id)
            redirect(controller:'customer', action:show, id:payInstance.customer.id)
        }
        else {
            render(view:'create',model:[payInstance:payInstance])
        }
    }

}
